import javax.xml.soap.Node;

public class LinkNode {
    public static LinkNode head=new LinkNode();
    public int data;
    public LinkNode next;

    public LinkNode(){}
    public LinkNode(int data){
        this.data=data;
    }
    public LinkNode(int data,LinkNode next){
        this.data=data;
        this.next=next;
    }

    /**
     * 添加节点
     * @param value 新节点值
     */
    public static void addNode(int value){
        LinkNode node = new LinkNode(value);
        LinkNode temp = head;
        while(temp.next!=null){
            temp = temp.next;
        }
        temp.next = node;
    }

    /**
     * 遍历链表
     */
    public static void traverseNode(LinkNode head){
        LinkNode temp = head.next;
        while(temp.next!=null){
            System.out.println(temp.data);
        }
        temp=temp.next;
    }

    /**
     * 插入节点
     */
    public static void insertNode(LinkNode node,int index,int value){
        if (index<1){
            System.out.println("插入位置不合法");
        }
        LinkNode node1 =new LinkNode(value);
        LinkNode temp = head;
        int currentIndex=0;
        while(temp.next!=null){
            if (index-1==currentIndex){
                node1.next=temp.next;
                temp.next=node1;
            }
            temp=temp.next;
            currentIndex++;
        }

    }

    /**
     * 获取链表长度
     */
    public static int getLength(LinkNode head){
        int length=0;
        LinkNode temp = head.next;
        while(temp.next!=null){
            temp=temp.next;
            length++;
        }
        return length;
    }

    /**
     * 删除链表节点
     */
    public static void deleteNode(LinkNode head,int index){
        if (index<1||index>getLength(head)+1){
            System.out.println("删除节点位置不合法");
        }
        LinkNode temp = head;
        int countNode=0;
        while(temp.next!=null){
            if (index-1==countNode){
                LinkNode node1=temp.next;
                temp.next =node1.next;
                node1=null;
            }
            temp=temp.next;
            countNode++;
        }
    }

    /**
     * 链表排序
     */
    public static void sortLinknode(LinkNode head){
        LinkNode currentNode;
        LinkNode nextNode;
        for (currentNode=head.next;currentNode.next!=null;currentNode=currentNode.next){
            for (nextNode=head.next;nextNode.next!=null;nextNode=nextNode.next) {
                if (nextNode.data > nextNode.next.data) {
                    int temp = nextNode.data;
                    nextNode.data = nextNode.next.data;
                    nextNode.next.data = nextNode.data;
                }
            }

        }
    }

    /**
     * 链表反转
     */
    public static void reverseLinknode(LinkNode head){
        LinkNode cur=head;
        LinkNode pre=null;
        while(cur!=null){
            LinkNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }

    }
}
